package com.chongxinlaiguo;

import com.alibaba.fastjson.JSON;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @Desc:
 * @Author: dengyanliang
 * @Date: 2025/9/5 14:29
 */
public class ListTest {
    public static void main(String[] args) {
        List<DiversionConfigAndRuleEntity> diversionConfigAndRuleEntityList  = new ArrayList<>();

        DiversionConfigAndRuleEntity diversionConfigAndRuleEntity = new DiversionConfigAndRuleEntity();
        diversionConfigAndRuleEntity.setId(1L);
        diversionConfigAndRuleEntity.setRegion("region");

        DiversionConfigAndRuleEntity diversionConfigAndRuleEntity2 = new DiversionConfigAndRuleEntity();
        diversionConfigAndRuleEntity2.setId(2L);
        diversionConfigAndRuleEntity2.setRegion("region");

        DiversionConfigAndRuleEntity diversionConfigAndRuleEntity3 = new DiversionConfigAndRuleEntity();
        diversionConfigAndRuleEntity3.setId(3L);
        diversionConfigAndRuleEntity3.setRegion("region");

        diversionConfigAndRuleEntityList.add(diversionConfigAndRuleEntity);
        diversionConfigAndRuleEntityList.add(diversionConfigAndRuleEntity2);
        diversionConfigAndRuleEntityList.add(diversionConfigAndRuleEntity3);

        Long diversionConfigId = 1L;

        DiversionConfigAndRuleEntity diversionConfigAndRuleEntity4 = new DiversionConfigAndRuleEntity();
        diversionConfigAndRuleEntity4.setId(1L);
        diversionConfigAndRuleEntity4.setRegion("region2======");

        // jdk 1.8 才支持 removeIf，之前的版本只能用 iterator 迭代器进行删除
        diversionConfigAndRuleEntityList.removeIf(next -> Objects.equals(next.getId(), diversionConfigId));

        // 老版本是这种方式遍历并修改数据
//        diversionConfigAndRuleEntityList.iterator().forEachRemaining(entity -> {
//            if (Objects.equals(entity.getId(), diversionConfigId)) {
//                diversionConfigAndRuleEntityList.remove(entity);
//            }
//        });
        diversionConfigAndRuleEntityList.add(diversionConfigAndRuleEntity4);

        // 直接遍历list，不能修改list，否则会报ConcurrentModificationException
//        for (DiversionConfigAndRuleEntity entity : diversionConfigAndRuleEntityList) {
//            if (Objects.equals(entity.getId(), diversionConfigId)) {
//                diversionConfigAndRuleEntityList.remove(entity);
//            }
//        }

        System.out.println(JSON.toJSONString(diversionConfigAndRuleEntityList));
    }
}
